package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.ApiOrgApplyHongSheng;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeHongSheng;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

/**
 *
 * 安心-泓晟-淄博全量20api - 数据无需加密
 * 链接： https://apifox.com/apidoc/shared-03482cc3-1276-47da-ba31-1479799b49a8
 * 访问密码：jsdPHMJw
 *
 * @author gwc
 * @create 2025-08-07 下午8:01
 */
@ApiPlan(productId = "395", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5HongSheng implements ApiOrgPhoneMd5ConvertHandler {

    private static final String BRAND = "1631";
    private static final String SOURCE = "ryd-ax";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        // 创建撞库实体类
        OrgDataExchangeHongSheng data = new OrgDataExchangeHongSheng();
        // 品牌ID
        data.setBrand(BRAND);
        // 手机号MD5字符串
        data.setMd5mobile(apiDataExchangeParameter.getPhoneMd5());
        // 转成json
        return  JsonUtils.toJson(data);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("data"), 1)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {

        //创建对应进件实体类
        ApiOrgApplyHongSheng hongShengData = new ApiOrgApplyHongSheng();
        // 拓展
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        // 来源（必需）
        hongShengData.setSource(SOURCE);
        // 姓名（必需）
        hongShengData.setName(apiApplyParameter.getUserName());
        // 手机号（必需）
        hongShengData.setMobile(apiApplyParameter.getPhone());
        // 城市（必需），不包含“市”，比如“杭州”
        hongShengData.setCity(apiApplyParameter.getCity().replace("市", ""));
        // 贷款额度（必需），单位为万，例如：20
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 3)
                .when(apiApplyParameter.getLoanAmount(), "2", 5)
                .when(apiApplyParameter.getLoanAmount(), "3", 10)
                .when(apiApplyParameter.getLoanAmount(), "4", 20)
                .end(3);
        hongShengData.setMoney(money);
        // 年龄（可选）
        hongShengData.setAge(apiFieldConverter.getAgeInt());
        // 性别（可选），“男”或“女”
        String sex = ChooseEq.create(apiApplyParameter.getSex())
                .when("0", "男")
                .when("1", "女")
                .end("男");
        hongShengData.setSex(sex);
        // 房（可选），“有”或“无”
        String house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", "无")
                .when(apiApplyParameter.getRealEstateInfo(), "2", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "3", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "4", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "5", "有")
                .when(apiApplyParameter.getRealEstateInfo(), "6", "有")
                .end("无");
        hongShengData.setHouse(house);
        // 车（可选），“有”或“无”
        String car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", "无")
                .when(apiApplyParameter.getCarInfo(), "2", "有")
                .when(apiApplyParameter.getCarInfo(), "3", "有")
                .when(apiApplyParameter.getCarInfo(), "4", "有")
                .end("无");
        hongShengData.setCar(car);
        // 社保（可选），“有”或“无”
        String shebao = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "无")
                .when(apiApplyParameter.getSocialSecurity(), "2", "有")
                .when(apiApplyParameter.getSocialSecurity(), "3", "有")
                .when(apiApplyParameter.getSocialSecurity(), "4", "有")
                .end("无");
        hongShengData.setShebao(shebao);
        // 公积金（可选），“有”或“无”
        String gongjijin = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", "无")
                .when(apiApplyParameter.getProvidentFund(), "2", "有")
                .when(apiApplyParameter.getProvidentFund(), "3", "有")
                .when(apiApplyParameter.getProvidentFund(), "4", "有")
                .end("无");
        hongShengData.setGongjijin(gongjijin);
        // 工作（可选），“有”或“无”
        String job = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", "有")
                .when(apiApplyParameter.getCareer(), "2", "有")
                .when(apiApplyParameter.getCareer(), "3", "有")
                .when(apiApplyParameter.getCareer(), "4", "有")
                .when(apiApplyParameter.getCareer(), "5", "有")
                .end("无");
        hongShengData.setJob(job);
        // 保单（可选），“有”或“无”
        String baodanIs = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", "无")
                .when(apiApplyParameter.getInsurancePolicy(), "2", "有")
                .when(apiApplyParameter.getInsurancePolicy(), "3", "有")
                .when(apiApplyParameter.getInsurancePolicy(), "4", "有")
                .end("无");
        hongShengData.setBaodan_is(baodanIs);
        // 信用卡（可选），“有”或“无”
        String creditLimit = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", "无")
                .when(apiApplyParameter.getCreditLimit(), "2", "有")
                .when(apiApplyParameter.getCreditLimit(), "3", "有")
                .when(apiApplyParameter.getCreditLimit(), "4", "有")
                .when(apiApplyParameter.getCreditLimit(), "5", "有")
                .when(apiApplyParameter.getCreditLimit(), "6", "有")
                .end("无");
        hongShengData.setCredit_card(creditLimit);
        // 是否有企业（可选），“有”或“无”
        hongShengData.setCompany("无");
        // 银行代发（可选），“银行代发”或“无”
        String salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发")
                .when(apiApplyParameter.getSalaryPayMethod(), "2", "无")
                .when(apiApplyParameter.getSalaryPayMethod(), "3", "无")
                .end("无");
        hongShengData.setIsbankpay(salaryPayMethod);
        // 芝麻分（可选）
        Integer sesameCredit = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 600)
                .when(apiApplyParameter.getSesameCredit(), "2", 649)
                .when(apiApplyParameter.getSesameCredit(), "3", 699)
                .when(apiApplyParameter.getSesameCredit(), "4", 700)
                .end(600);
        hongShengData.setZhima(sesameCredit);

        return JsonUtils.toJson(hongShengData);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
